package com.shengzai.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

public class ZookeeperGetChdCode {
    public static void fun(String paths) throws IOException, InterruptedException, KeeperException {
        Watcher watcher = new Watcher(){
            @Override
            public void process(WatchedEvent event){
                System.out.println("process函数正在运行");
            }
        };
        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181", 10000, watcher);
        Stat exists = zooKeeper.exists(paths, false);
        int numChildren = exists.getNumChildren();
        if (numChildren>0){
            List<String> children = zooKeeper.getChildren(paths, false);
            for (String child : children) {
                System.out.println(paths+"的子节点为:"+child);
                String tem = paths+"/"+child;
                fun(tem);
            }
        }else {
            System.out.println(paths+"节点没有子节点啦~~~");
        }
        zooKeeper.close();
    }
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        fun("/first_node");
    }

}
